package com.rongshu.view.userInfo.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.rongshu.R;
import com.rongshu.utils.CharacterParser;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.userInfo.activity.CitySelectActivity;
import com.rongshu.view.userInfo.adapter.InlandAdpter;
import com.rongshu.view.userInfo.comparator.PinyinComparator;
import com.rongshu.view.userInfo.entity.Province;
import com.rongshu.view.userInfo.presenter.InlandPresenter;
import com.rongshu.view.userInfo.iview.InlandView;
import com.widget.city.view.LetterView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

import static android.app.Activity.RESULT_OK;

public class InlandFragment extends BaseFragment<InlandPresenter> implements InlandView, InlandAdpter.OnItemClickLintener {
    @BindView(R.id.province_recyclerview)
    RecyclerView province_recyclerview;
    @BindView(R.id.letterView)
    LetterView letterView;

    //汉字转拼音类
    private CharacterParser characterParser;
    //拼音排序
    private PinyinComparator pinyinComparator;
    //list
    private List<Province.ResponseBean> provinceList = null;
    //adapter
    private InlandAdpter mInlandAdpter = null;
    //manager
    private LinearLayoutManager manager = null;
    //request
    private final int CITY_SELECT_REQUEST = 0x01;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_inland_view;
    }

    @Override
    protected void initViews() {
        //presenter
        setPresenter(new InlandPresenter(getActivity()), this);
        //实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();
        //拼音排序
        pinyinComparator = new PinyinComparator();
        //list
        provinceList = new ArrayList<>();
        //adapter
        mInlandAdpter = new InlandAdpter(getActivity());
        mInlandAdpter.setHasStableIds(true);
        mInlandAdpter.setOnItemClickLintener(this);
        //manager
        manager = new LinearLayoutManager(getActivity());
        //recylcerview
        province_recyclerview.setLayoutManager(manager);
        province_recyclerview.setAdapter(mInlandAdpter);
    }

    @Override
    protected void initData() {
        //设置右侧触摸监听
        letterView.setOnTouchingLetterChangedListener(new LetterView.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                if (s.contains("#")) {
                    province_recyclerview.smoothScrollToPosition(0);
                    return;
                }
                //该字母首次出现的位置
                int position = mInlandAdpter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    province_recyclerview.smoothScrollToPosition(position);
                }
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        //获取省份
        Map<String, Object> map = new HashMap<>();
        map.put("rnd", "0");
        presenter.getChinaAddr(
                (String) map.get("rnd"),
                Common.encryptionSign(map));
    }

    @Override
    public void onChinaAddr(Province province) {
        for (Province.ResponseBean responseBean : province.getResponse()) {
            String pinyin = characterParser.getSelling(responseBean.getTitle());
            String sortString = pinyin.substring(0, 1).toUpperCase();
            if (sortString.matches("[A-Z]")) {
                responseBean.setSortLetters(sortString.toUpperCase());
                responseBean.setType(InlandAdpter.TYPE_LETTER);
            } else {
                responseBean.setSortLetters("#");
                responseBean.setType(InlandAdpter.TYPE_HEADER);
            }
            provinceList.add(responseBean);
        }

        Collections.sort(provinceList, pinyinComparator);

        if (mInlandAdpter != null) {
            mInlandAdpter.setProvinceList(provinceList);
            mInlandAdpter.notifyDataSetChanged();
        }
    }

    /**
     * 输入框输入
     *
     * @param content
     */
    @Override
    public void onEditInput(String content) {
        List<Province.ResponseBean> responseBeanList = new ArrayList<>();
        if (!TextUtils.isEmpty(content)) {
            for (Province.ResponseBean responseBean : this.provinceList) {
                String province = responseBean.getTitle();
                if (province.contains(content)) {
                    responseBeanList.add(responseBean);
                }
            }
        } else {
            responseBeanList = provinceList;
        }

        if (mInlandAdpter != null) {
            mInlandAdpter.setProvinceList(responseBeanList);
            mInlandAdpter.notifyDataSetChanged();
        }
    }

    /**
     * item点击
     *
     * @param position
     */
    @Override
    public void onItemClick(int position) {
        Province.ResponseBean responseBean = null;
        if (mInlandAdpter != null) {
            responseBean = mInlandAdpter.getProvinceList().get(position);
            Intent intent = new Intent(getActivity(), CitySelectActivity.class);
            intent.putExtra(Constant.INTENT_DATA, responseBean);
            startActivityForResult(intent, CITY_SELECT_REQUEST);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CITY_SELECT_REQUEST://城市选择返回
                    String city = data.getStringExtra(Constant.INTENT_MESSAGE);
                    Intent intent = new Intent();
                    intent.putExtra(Constant.INTENT_MESSAGE, city);
                    getActivity().setResult(RESULT_OK, intent);
                    getActivity().finish();
                    break;
            }
        }
    }
}
